﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BOM_ITEM
// * CreateDate    : 2013-11-25 16:35:23
// * Creater       : CodeSmith
// * LastChangeDate: 2013-11-25 16:35:23
// * LastChanger   : 2013-11-25 16:35:23
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Bpt
{
    /// <summary>
    /// BomItem
    /// </summary>
    public partial class BomItemBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomItemEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomItemEntity GetBomItem(BomItemEntity objBomItemEntity)
        {
            return DataAccess.SelectSingle<BomItemEntity>(objBomItemEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBomItemEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomItem(BomItemEntity objBomItemEntity)
        {
            return DataAccess.Insert(objBomItemEntity);
        }

        public static bool InsertBomItem(List<BomItemEntity> objBomItemEntityList)
        {
            return DataAccess.Insert(objBomItemEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomItemEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomItem(BomItemEntity objBomItemEntity)
        {
            return DataAccess.Update(objBomItemEntity);
        }

        public static bool UpdateBomItem(List<BomItemEntity> objBomItemEntityList)
        {
            return DataAccess.Update<BomItemEntity>(objBomItemEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomItemEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomItem(BomItemEntity objBomItemEntity)
        {
            return DataAccess.Delete(objBomItemEntity);
        }

        public static bool DeleteBomItem(List<BomItemEntity> objBomItemEntityList)
        {
            return DataAccess.Delete<BomItemEntity>(objBomItemEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomItemAll()
        {
            return DataAccess.SelectAllDataSet<BomItemEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomItemEntity> GetBomItemAllList()
        {
            return DataAccess.SelectAll<BomItemEntity>();
        }
        #endregion

        public static List<BomItemEntity> GetBomItemAllList(BomItemEntity bomItem)
        {
            return DataAccess.Select<BomItemEntity>(bomItem);
        }
        
        public static DataSet GetBomItemByMasterId(string masterId)
        {
            if (string.IsNullOrEmpty(masterId))
                return null;
            string sql = @"select bi.*,mp.PRODUCT_NAME,mp.PRODUCT_CODE
                from BOM_ITEM bi
                left join MST_PRODUCT mp on bi.MTRL_ID=mp.PRODUCT_ID
                where bi.BOM_MASTER_ID='" + masterId + "' ";
            return DataAccess.SelectDataSet(sql);
        }

        public static List<BomItemEntity> GetListBomItemByMasterId(string masterId)
        {
            if (string.IsNullOrEmpty(masterId))
                return null;
            return DataAccess.Select<BomItemEntity>(new BomItemEntity { BomMasterId = masterId });
        }

        public static bool SaveBomItem(List<BomItemEntity> adds, List<BomItemEntity> updateds, List<BomItemEntity> dels)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Delete(dels, broker);
                    DataAccess.Update(updateds, broker);
                    DataAccess.Insert(adds, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool SaveBomItem(List<BomItemEntity> adds)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                string bomMasterId = adds[0].BomMasterId;
                try
                {
                    broker.BeginTransaction();
                    List<BomItemEntity> dels = GetListBomItemByMasterId(bomMasterId);
                    DataAccess.Delete(dels, broker);
                    DataAccess.Insert(adds, broker);
                    
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

    }


}

